#coding:utf-8
from django.shortcuts import render_to_response
from models import Template
 

from google.appengine.api import users

def preview(authInfo, Reply_to, isUsed_gdata, Macro, ToMail, \
            MailTitle, MailContext, isSendMyself, nMailCol, \
            isSendOntime, SendingTime, gdataInfo, arrMailList):
    user = users.GetCurrentUser()
    if not user:
        greeting = ("<a href=\"%s\">需要登录Google帐号，点击进行登录</a>." % 
                    users.create_login_url("/"))
        
    fck = {}
    fck['value'] = MailContext
    
    templateList = []
    
    userTemp = Template.gql("WHERE owner =:owner", owner = user)
    for temp in userTemp:  #  
        t = {}
        t['id'] = temp.key()
        t['name'] = temp.temp_name
        templateList.append(t)
        
    template = {}    
    template['sender'] = user.email()
    template['macro'] = Macro
    template['subject'] = MailTitle
    template['reply_to'] = Reply_to
    template['mail_list'] = ToMail
    template['glogin_mail'] = gdataInfo['gLogMail']
    template['gFileName'] = gdataInfo['gFileName']
    template['gPsw'] = gdataInfo['gPsw']
    template['wksht_name'] = gdataInfo['wksht_name']
    template['nColMail'] = nMailCol + 1
    template['delete'] = 'none'
    
    if isUsed_gdata:
        template['usedgdata'] = 'checked="CHECKED"'
 
    if isSendMyself:
        template['isSendMyself'] = 'checked="CHECKED"'
    if isUsed_gdata:
        template['usedgdata'] = 'checked="CHECKED"'
    template['nMailSum'] = "nSum = %s;" % len(arrMailList)


    #宏使用 逗号隔开
    listMacro = Macro.strip().split(',')
    gdata = ''
    titles = '['
    mailContexts  = '['
    for line in arrMailList:#一个二维list，
        title = MailTitle
        mailHtml = MailContext
        nIdx = 0
        for macro in listMacro:
            macro = macro.strip() #去两边空格
            if macro == '':
                print "macro is emtpy!"
                continue
            if line.__len__() > nIdx:
                strTemp = line[nIdx].strip()
                if strTemp == '':
                    gdata += " NULL, "
                else:
                    gdata += strTemp + ', '
                if type(strTemp) is unicode: strTemp = strTemp.encode('utf8')
                title = title.replace(macro, strTemp)
                mailHtml = mailHtml.replace(macro, strTemp)
                nIdx +=1
            else:
                gdata += " NULL, "
                title = title.replace(macro, '')
                mailHtml = mailHtml.replace(macro, '')
        gdata += '<br>'        
        #把特殊字符替换                    
        titles += "['" + title.replace("'", r"\'").replace('"', r'\"') + "'],"
        mailContexts += "['" + mailHtml.replace("'", r"\'").replace('"', r'\"').replace("\r", r"\r").replace("\n", r"\n")  + "'],"

    template['gdata'] = gdata
    template['titles'] =  "var arrTitles = %s];" % titles
    template['contexts'] = 'var arrContexts = %s];' % mailContexts

    return render_to_response('editmail.html', {'templateList': templateList,'fck': fck, "template":template})

#end index()


if __name__ == '__main__':
    pass